package co.fitstart.admin.web.controller.content;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import co.fitstart.admin.entity.content.ActivitySearch;
import co.fitstart.admin.service.ActivityService;
import co.fitstart.admin.web.interceptor.MenuSetting;
import co.fitstart.entity.content.Activity;

import com.demo2do.core.persistence.support.Order;
import com.demo2do.core.support.Page;
import com.demo2do.core.support.Result;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.resolver.Preloading;
import com.demo2do.core.web.utils.WebUtils;

/**
 * 
 * @author mohanlan
 *
 */
@Controller
@RequestMapping("/activities")
@MenuSetting("menu-content")
public class ActivityController {
    
    private static final Logger logger = LoggerFactory.getLogger(ActivityController.class);
    
    @Autowired
    @Qualifier("activityService")
    private ActivityService activityService;
    
    /**
     * List all the activities by page
     * 
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-activity-management")
    public ModelAndView list(Page page) {
        
        ModelAndView modelAndView = new ModelAndView("content/activity/activity-list");

        List<Activity> activities = activityService.listByPage(Activity.class, 
                                                               new Order("serial", "DESC").add("createTime", "DESC").add("status", "DESC"),
                                                               page);
        
        modelAndView.addObject("activities", activities);
        modelAndView.addObject("page", page);
        
        return modelAndView;
    }
    
    /**
     * Display search panel
     * 
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public String search() {
        return "content/activity/activity-search";
    }
    
    /**
     * @param activitySearch
     * @param request
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, params = "filter")
    @MenuSetting("submenu-activity-management")
    public ModelAndView onSearch(ActivitySearch activitySearch,
                                 HttpServletRequest request, 
                                 Page page) {
        
        ModelAndView modelAndView = new ModelAndView("content/activity/activity-list");
        
        List<Activity> activities = activityService.search(Activity.class, activitySearch, page);
        
        modelAndView.addObject("activities", activities);
        modelAndView.addObject("page", page);
        
        modelAndView.addObject("refer", "search");
        modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page", "filter"));
        
        return modelAndView;
    }
    
    /**
     * Display create panel
     * 
     * @return
     */
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    @MenuSetting("submenu-activity-management")
    public String create(){
        
        return "content/activity/activity-create";
    }
    
    /**
     * Do create
     * 
     * @param activity
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    public @ResponseBody String onCreate(Activity activity){
        
        Result result = new Result();
        
        try{
            
            activityService.save(activity.initialize());
            
            result.success().message("创建活动成功");
        
        }catch(Exception e){
            
            logger.error("ActivityController#onCreate() error:", e);
            
            result.fail().message("创建活动失败");
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    /** 
     * Prepare to delete activity
     * 
     * @param activityId
     * @return
     */
    @RequestMapping(value = "/{activityId}/delete", method = RequestMethod.GET)
    public ModelAndView delete(@PathVariable("activityId") Long activityId){
      
      ModelAndView modelAndView = new ModelAndView("content/activity/activity-delete");
      
      modelAndView.addObject("activityId", activityId);
      
      return modelAndView;
    
    }
    
    /**
     * Do delete activity
     * 
     * @param activityId
     * @return
     */
    @RequestMapping(value = "/{activityId}", method = RequestMethod.DELETE)
    public @ResponseBody String onDelete(@PathVariable("activityId") Long activityId) {
        
        Result result = new Result();
        
        try {
            
            Activity activity = activityService.load(Activity.class, activityId);
            
            activityService.delete(activity);
            
            result.success().message("删除活动成功!");
            
        } catch (Exception e) {
            
            logger.error("ActivityController#onDelete() error:", e);
            
            result.fail().message("删除活动失败");
        }
        
        return JsonUtils.toJsonString(result);
        
    }
    
    /** 
     * Prepare to online activity
     * 
     * @param activityId
     * @return
     */
    @RequestMapping(value = "/{activityId}/online", method = RequestMethod.GET)
    public ModelAndView online(@PathVariable("activityId") Long activityId){
      
        ModelAndView modelAndView = new ModelAndView("content/activity/activity-online");
      
        modelAndView.addObject("activityId", activityId);
        
        return modelAndView;
    
    }
    
    /**
     * Do online activity
     * 
     * @param activityId
     * @return
     */
    @RequestMapping(value = "/{activityId}/online", method = RequestMethod.PATCH)
    public @ResponseBody String onOnline(@PathVariable("activityId") Long activityId) {
        
        Result result = new Result();
        
        try {
            
            Activity activity = activityService.load(Activity.class, activityId);
            
            activityService.online(activity);
            
            result.success().message("上架活动成功!");
            
        } catch (Exception e) {
            
            logger.error("ActivityController#onOnline() error:", e);
            
            result.fail().message("上架活动失败!");
        }
        
        return JsonUtils.toJsonString(result);
        
    }
    
    /**
     * Prepare to stick
     * 
     * @param activityId
     * @return
     */
    @RequestMapping(value = "/{activityId}/stick", method = RequestMethod.GET)
    public ModelAndView stick(@PathVariable("activityId") Long activityId) {
        
        ModelAndView modelAndView = new ModelAndView("content/activity/activity-stick");
        
        modelAndView.addObject("activityId", activityId);
        
        return modelAndView;
    }
    
    /**
    * Do stick activity;
    * 
    * @param activityId
    * @return
    */
   @RequestMapping(value = "/{activityId}/stick", method = RequestMethod.PATCH)
   public @ResponseBody String onStick(@PathVariable("activityId") Long activityId) {
       
       Result result = new Result();
       
       try {
           
           Activity activity = activityService.load(Activity.class, activityId);
           
           activityService.stick(activity, 4);
           
           result.success().message("置顶活动成功");
           
       } catch (Exception e) {
           
           logger.error("ActivityController#onUpSerial() error:", e);
           
           result.fail().message("置顶活动失败!");
       }
       
       return JsonUtils.toJsonString(result);
       
   }
   
   /**
    * Prepare to unstick
    * 
    * @param activityId
    * @return
    */
   @RequestMapping(value = "/{activityId}/unstick", method = RequestMethod.GET)
   public ModelAndView unStick(@PathVariable("activityId") Long activityId) {
       
       ModelAndView modelAndView = new ModelAndView("content/activity/activity-unstick");
       
       modelAndView.addObject("activityId", activityId);
       
       return modelAndView;
   }
   
   /**
   * Do unstick activity ;
   * 
   * @param activityId
   * @return
   */
  @RequestMapping(value = "/{activityId}/unstick", method = RequestMethod.PATCH)
  public @ResponseBody String onUnstick(@PathVariable("activityId") Long activityId) {
      
      Result result = new Result();
      
      try {
          
          Activity activity = activityService.load(Activity.class, activityId);
          
          activityService.unstick(activity);
          
          result.success().message("取消置顶成功");
          
      } catch (Exception e) {
          
          logger.error("ActivityController#onDownSerial() error:", e);
          
          result.fail().message("取消置顶成功");
      }
      
      return JsonUtils.toJsonString(result);
      
  }
  
  /**
   * View activity detail page
   * 
   * @param activityId
   * @return
   */
  @RequestMapping("/{activityId}")
  @MenuSetting("submenu-activity-management")
  public ModelAndView view(@PathVariable("activityId") Long activityId) {
      
      ModelAndView modelAndView = new ModelAndView("content/activity/activity-view");
      
      Activity activity = activityService.load(Activity.class, activityId);
      
      modelAndView.addObject("activity", activity);
      
      return modelAndView;
  }
  
  /**
   * Display edit panel
   * 
   * @param activityId
   * @return
   */
  @RequestMapping(value = "/{activityId}/edit", method = RequestMethod.GET)
  public ModelAndView edit(@PathVariable("activityId") Long activityId){
      
      ModelAndView modelAndView = new ModelAndView("content/activity/activity-edit");
      
      Activity activity = activityService.load(Activity.class, activityId);
      
      modelAndView.addObject("activity", activity);
      
      return modelAndView;
  }
  
  /**
   * Do edit
   * 
   * @param activity
   * @return
   */
  @RequestMapping(value = "/{activityId}", method = RequestMethod.PATCH)
  public @ResponseBody String onEdit(@Preloading("activityId") Activity activity){
      
      Result result = new Result();
      
      try{
          
          activityService.update(activity);
          
          result.success().message("修改活动成功");
          
      }catch(Exception e){
          
          logger.error("ActivityController#onEdit() error:", e);
          
          result.fail().message("修改活动失败");
      }
      
      return JsonUtils.toJsonString(result);
  }
  
  /**
   * Display edit cover panel
   * 
   * @param activityId
   * @return
   */
  @RequestMapping(value = "/{activityId}/cover", method = RequestMethod.GET)
  public ModelAndView editCover(@PathVariable("activityId") Long activityId){
      
      ModelAndView modelAndView = new ModelAndView("content/activity/activity-cover-edit");
      
      Activity activity = activityService.load(Activity.class, activityId);
      
      modelAndView.addObject("activity", activity);
      
      return modelAndView;
  }
  
  /**
   * Do edit cover
   * 
   * @param activityId
   * @param cover
   * @return
   */
  @RequestMapping(value = "/{activityId}/cover", method = RequestMethod.PATCH)
  public @ResponseBody String onEditCover(@PathVariable("activityId") Long activityId, String cover){
      
      Result result = new Result();
      
      try{
          
          Activity activity = activityService.load(Activity.class, activityId);
          
          activityService.update(activity.editCover(cover));
          
          result.success().message("修改活动封面成功");
          
      }catch(Exception e){
          
          logger.error("ActivityController#onEdit() error:", e);
          
          result.fail().message("修改活动封面失败");
      }
 
      return JsonUtils.toJsonString(result);
  }
  
}
